<template>
  <avue-form :option="option" v-model="form" ref="formRef" @submit="handleSubmit"> </avue-form>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import useFormOption from "../combination/useFormOption";
import { addApi, updateApi, getDetailApi } from "@/api/homeSet/messageListApi";
import { ElMessage } from "element-plus";

const { id, ntype } = defineProps(["id", "ntype"]);
const emit = defineEmits(["done"]);

const formRef = ref();
const form = ref<any>();

const option = useFormOption();

async function handleSubmit(data: any, done: () => void) {
  if (ntype === "add") {
    await addApi(data);
  }
  if (ntype === "edit") {
    await updateApi(data);
  }
  ElMessage.success("操作成功");
  emit("done");
  done();
}

function submit() {
  formRef.value.submit();
}

/**
 * 请求数据
 */
onMounted(async () => {
  if (["edit", "view"].includes(ntype)) {
    const res = await getDetailApi(id);
    setTimeout(() => {
      form.value = res.data;
    }, 0);
  }
});

defineExpose({
  submit,
});
</script>
